﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WorkSpace.UI.WorkSpaceRef;
using System.ComponentModel;
using SmartSpace.UI.Silverlight;

namespace WorkSpace.UI.Controls.Windows
{
    public partial class AssignWindow : SlWindow
    {
        private List<SelectItem<Link>> _items;

        private Category _category;
        private PersonalPackage _package;
        private string _account;

        public AssignWindow(Category category, PersonalPackage package, string account)
        {
            InitializeComponent();

            _category = category;
            _package = package;
            _account = account;

            this.Loaded += new RoutedEventHandler(AssignWindow_Loaded);
        }

        #region 加载数据
        void AssignWindow_Loaded(object sender, RoutedEventArgs e)
        {
            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.GetAllLinksCompleted += new EventHandler<GetAllLinksCompletedEventArgs>(client_GetAllLinksCompleted);
            client.GetAllLinksAsync(_category.Id, _account);
        }

        void client_GetAllLinksCompleted(object sender, GetAllLinksCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                _items = new List<SelectItem<Link>>();

                foreach (Link item in e.Result.Data)
                {
                    _items.Add(new SelectItem<Link>(item));
                }

                WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
                client.GetPersonalLinksCompleted += new EventHandler<GetPersonalLinksCompletedEventArgs>(client_GetPersonalLinksCompleted);
                client.GetPersonalLinksAsync(_package.Id);
            }
        }

        void client_GetPersonalLinksCompleted(object sender, GetPersonalLinksCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                foreach (SelectItem<Link> item in _items)
                {
                    foreach (PersonalLink pLink in e.Result.Data)
                    {
                        if (pLink.LinkId == item.Data.Id)
                        {
                            item.IsSelected = true;
                            break;
                        }
                    }
                }

                _ctlGrid.ItemsSource = _items;
            }
        }
        #endregion

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ArrayOfGuid ids = new ArrayOfGuid();
            foreach (SelectItem<Link> item in _items)
            {
                if (item.IsSelected)
                {
                    ids.Add(item.Data.Id);
                }
            }
            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.AddPersonalLinksCompleted += new EventHandler<AddPersonalLinksCompletedEventArgs>(client_AddPersonalLinksCompleted);
            client.AddPersonalLinksAsync(_package.Id, ids);
        }

        void client_AddPersonalLinksCompleted(object sender, AddPersonalLinksCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }

    public class SelectItem<T> : INotifyPropertyChanged
    {
        private bool _isSelected;

        public T Data { get; set; }
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;
                    RaisePropertyChanged("IsSelected");
                }
            }
        }

        public SelectItem(T data)
        {
            Data = data;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

